package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加功能
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        Long categoryId = dishDto.getCategoryId();
        String DishName="Dish_"+categoryId+"_1";
        redisTemplate.delete(DishName);
        log.info(dishDto.toString());
        log.info(dishDto.getCategoryId().toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 分页及条件查询分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name){
        Page<DishDto> dishPage = dishService.pageWithCategoryName(page, pageSize, name);
        return R.success(dishPage);
    }

    /**
     * 菜品编辑回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> edit(@PathVariable Long id){
        Dish dish = dishService.getById(id);
        String categoryName = categoryService.getById4Type(dish.getCategoryId());
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<>();
        lqw.eq(dish.getId()!=null,DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        dishDto.setCategoryName(categoryName);
        dishDto.setFlavors(flavors);
        return R.success(dishDto);
    }

    /**
     * 菜品修改
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        dishService.updateWithFlavor(dishDto);
        Long categoryId = dishDto.getCategoryId();
        String DishName="Dish_"+categoryId+"_1";
        redisTemplate.delete(DishName);
        return R.success("菜品修改成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId){
        String dishName="Dish_"+categoryId+"_1";
        List<DishDto> list = (List<DishDto>) redisTemplate.opsForValue().get(dishName);
        if(list!=null){
            return R.success(list);
        }
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        lqw.eq(Dish::getStatus,1);
        List<Dish> dishList = dishService.list(lqw);
        List<DishDto> dishDtos = dishList.stream().map(item -> {
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(item.getId() != null, DishFlavor::getDishId, item.getId());
            List<DishFlavor> dishFlavor = dishFlavorService.list(lambdaQueryWrapper);
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            dishDto.setFlavors(dishFlavor);
            return dishDto;
        }).collect(Collectors.toList());
        redisTemplate.opsForValue().set(dishName,dishDtos,60, TimeUnit.MINUTES);
        return R.success(dishDtos);
    }
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        dishService.deWithFlavor(ids);
        return R.success("删除菜品成功");
    }
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        dishService.updateStatus(status,ids);
        return R.success("状态修改成功");
    }
}
